<template>
  <div class="wrapper">
    <div class="head">
      <h3><span @click="goHome">返回</span>在线客服</h3>
    </div>
    <div class="main1 clearfix" id="answer"></div>
    <div class="footer">
      <input type="text" name="" id="ipt" @keyup.enter="handleClick" /><button
        @click="handleClick"
      >
        发送
      </button>
    </div>
  </div>
</template>

<style scoped>
* {
  padding: 0;
  margin: 0;
}
html,
body {
  height: 100%;
}

.clearfix {
  clear: both;
  content: "";
  display: block;
}
.wrapper {
  height: 100%;
  width: 380px;
  height: 812px;
  margin: 0 auto;
  box-sizing: border-box;
}

.wrapper .head {
  width: 100%;
  color: white;
  background-color: #64b587;
  text-align: center;
  height: 40px;
  line-height: 40px;
}

.wrapper .head span {
  position: absolute;
  left: 20px;
}

.wrapper .main1 {
  width: 100%;
  background-color: #eee;
  height: calc(100% - 90px);
  overflow-y: scroll;
  padding: 10px;
  box-sizing: border-box;
}

img {
  width: 40px;
  height: 40px;
  border-radius: 50%;
}

.text {
  padding: 10px;
  position: relative;
  margin-top: 10px;
  max-width: 150px;
  box-sizing: border-box;
}

.wrapper .main1 .user {
  float: right;
  width: 55%;
}

.wrapper .main1 .user img {
  float: right;
}

.wrapper .main1 .user .text {
  float: right;
  right: 6px;
  background-color: greenyellow;
}

.wrapper .main1 .robot .text {
  left: 6px;
  float: left;
  background-color: honeydew;
}

.wrapper .main1 .robot {
  float: left;
  width: 55%;
}

.wrapper .main1 .robot img {
  float: left;
}

.wrapper .footer {
  width: 100%;
  background-color: #ddd;
  height: 50px;
  line-height: 50px;
  text-align: center;
}

.wrapper .footer input {
  outline: none;
  width: 300px;
  height: 20px;
  line-height: 40px;
  padding: 5px;
  border: none;
}

.wrapper .footer button {
  outline: none;
  border: none;
  line-height: 30px;
  height: 30px;
  padding: 0 8px;
}
</style>

<script>
export default {
  data() {
    return {
      obj: {
        color: "red",
      },
    };
  },
  methods: {
    handleClick() {
      var input = document.getElementById("ipt");
      var text = input.value;
      console.log(text);
      if (text) {
        this.renderDom("mine", text);
        input.value = "";
        this.$axios({
          method: "get",
          url: "http://api.tianapi.com/txapi/robot/index",
          params: {
            key: "b6f1729656d1d9fe580848234c455f46",
            question: text,
          },
        }).then((res) => {
          console.log(res.data.newslist[0].reply);
          // res = JSON.parse(res);
          let data = res.data.newslist[0].reply;
          this.renderDom("robot", data);
        });
      }
    },
    renderDom(who, data) {
      let main = document.getElementById("answer");
      console.log(main);
      var div = document.createElement("div");
      div.innerHTML = `<div data-v-1ef58504 class="${
        who == "mine" ? "user" : "robot"
      }">
                    <img data-v-1ef58504 src="${
                      who == "mine"
                        ? "/images/WechatIMG1.png"
                        : "/images/3.png"
                    }" alt="">
                    <div data-v-1ef58504 class="text">${data}</div>
                  </div>`;
      main.appendChild(div);
      main.scrollTop = main.scrollHeight;
    },
    goHome() {
      this.$router.push({ name: "HomaePage" });
    },
  },
};
</script>